package tw.edu.tku.im.maze;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.json.JSONException;
import org.json.JSONObject;


public class Client implements DeliveryListenerI {
   
   Socket s;
   BufferedReader in ;
   PrintWriter out ;
   
   public Client() { 
      
   }
   
   public void testRegister(String userID, String name, String password) {
      final RegisterDeliverer d = new RegisterDeliverer();
      final RegisterReq req = new RegisterReq(userID, name, password);
      d.deliver(req, new DeliveryListenerI() {
         @Override public void processReply(RepPacket reply) {
            System.out.println(">>> Received: ");
            System.out.println(reply.toJSONString());
         }
      });
   }
   
   public void testLogin(String userID, String password) {
      final LoginDeliverer d = new LoginDeliverer();
      final LoginReq req = new LoginReq(userID, password);
      d.deliver(req, this);
   }
   
   public void testAddRoom(String room, String homeowner, int ticket, 
                           int mapLv, int mode, String roomPassword) 
   {
      final AddRoomDeliverer d = new AddRoomDeliverer();
      final AddRoomReq req = 
            new AddRoomReq(room, homeowner, ticket, mapLv, mode, roomPassword);
      d.deliver(req, this);
   }
   
   public void testEnterRoom(int roomID, String userID, int ticket, 
                             String roomPassword) 
   {
      final EnterRoomDeliverer d = new EnterRoomDeliverer();
      final EnterRoomReq req = 
            new EnterRoomReq(roomID, userID, ticket, roomPassword);
      d.deliver(req, this);
   }
   
   public void testLeaveRoom(int roomID, String userID, int ticket) {
      final LeaveRoomDeliverer d = new LeaveRoomDeliverer();
      final LeaveRoomReq req = new LeaveRoomReq(roomID, userID, ticket);
      d.deliver(req, this);
   }
   
   public void testChangeTeam(int roomID, String userID, int ticket) {
      final ChangeTeamDeliverer d = new ChangeTeamDeliverer();
      final ChangeTeamReq req = new ChangeTeamReq(roomID, userID, ticket);
      d.deliver(req, this);
   }
   
   public void testRoomList() {
      final RoomListDeliverer d = new RoomListDeliverer();
      final RoomListReq req = new RoomListReq();
      d.deliver(req, this);
   }
   
   public void testUpdateRoom(int roomID) {
      final UpdateRoomDeliverer d = new UpdateRoomDeliverer();
      final UpdateRoomReq req = new UpdateRoomReq(roomID);;
      d.deliver(req, this);
   }
   
   public void testStartGame(int roomID, String userID, int ticket) {
      final StartGameDeliverer d = new StartGameDeliverer();
      final StartGameReq req = new StartGameReq(roomID, userID, ticket);
      d.deliver(req, this);
   }
   
   public void testMark(int roomID, String userID, int ticket, String lastTimestamp,
                        int markX, int markY, boolean markAction) {
      final MarkDeliverer d = new MarkDeliverer();
      final MarkReq req = new MarkReq(roomID, userID, ticket, lastTimestamp, 
                                      new MazeCell(markX, markY), markAction);
      d.deliver(req, this);
   }
   
   public void testUpdateGame(int roomID, String userID, int ticket, String lastTimestamp) {
      final UpdateGameDeliverer d = new UpdateGameDeliverer();
      final UpdateGameReq req = new UpdateGameReq(roomID, userID, ticket, lastTimestamp);
      d.deliver(req, this);
   }
   
   public void testEndGame(int roomID, String userID, int ticket) {
      final EndGameDeliverer d = new EndGameDeliverer();
      final EndGameReq req = new EndGameReq(roomID, userID, ticket);
      d.deliver(req, this);
   }
   
   

   @Override public void processReply(RepPacket reply) {
      System.out.println(">>>> Received: ");
      System.out.println(reply.toJSONString());
   }
   
   public static void main(String[] args) throws JSONException {
      int test123Ticket = -1584041340;
      int test456Ticket = -1584011672;
      int test789Ticket = -1584011585;
      int john1022Ticket = 1208301060;
      Client client  = new Client();
//      client.testRegister("test789", "test", "789");
//      client.testLogin("test123", "123");
//      client.testAddRoom("room","test123", test123Ticket, 1, 0, null);
//      client.testChangeTeam(25, "test123", test123Ticket);
//      client.testEnterRoom(1, "test789", test789Ticket, null);
//      client.testLeaveRoom(25, "test123", test123Ticket);
//      client.testRoomList();
//      client.testUpdateRoom(2);
//      client.testStartGame(1, "test123", test123Ticket);
//      client.testMark(4, "test123", test123Ticket, "0000000000001782", 39, 40, Boolean.FALSE);
      client.testUpdateGame(4, "test123", test123Ticket, "0000000000001780");
//      client.testEndGame(2, "test123", test123Ticket);
   }
}
